package com.kim.useroauth.server.controller;

import com.kim.common.utilcom.model.Result;
import com.kim.useroauth.common.dto.*;
import com.kim.useroauth.server.service.IUserOauthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * 用户授权认证
 */
@Api(tags = {"app: 用户授权认证相关", "不检查是否授权"})
@Slf4j
@RestController
@RequestMapping("/app/v1/user/unAuthenticated")
public class UserAuthController {

    /**
     * 用户认证Service
     */
    @Resource
    private IUserOauthService userOauthService;

    /**
     * 密码模式
     * <p>用户名 + 密码登录</p>
     *
     * @param reqPasswordLoginDTO 用户名密码登录DTO
     */
    @ApiOperation(value = "用户名 + 密码登录", responseReference = "返回token")
    @PostMapping(value = "/loginByUsernameAndPassword")
    public Result loginByUsernameAndPassword(@Valid @RequestBody ReqPasswordLoginDTO reqPasswordLoginDTO) {
        return userOauthService.loginByUsernameAndPassword(
                reqPasswordLoginDTO.getUsername(), reqPasswordLoginDTO.getPassword(),
                null, null, reqPasswordLoginDTO.getAuthType(),
                reqPasswordLoginDTO.getClientId(), reqPasswordLoginDTO.getClientSecret(),
                reqPasswordLoginDTO.getScope()
        );
    }

    /**
     * 密码模式
     * <p>用户名 + 密码 + 验证码登录</p>
     *
     * @param reqVerificationCodeLoginDTO 用户名密码登录DTO
     */
    @ApiOperation(value = "用户名 + 密码 + 验证码登录", responseReference = "(建议不要使用验证码)返回token")
    @PostMapping(value = "/loginByUsernameAndPasswordAndVerificationCode")
    public Result<Object> loginByUsernameAndPasswordAndVerificationCode(@Valid @RequestBody ReqVerificationCodeLoginDTO reqVerificationCodeLoginDTO) {
        return userOauthService.loginByUsernameAndPassword(reqVerificationCodeLoginDTO.getUsername(),
                reqVerificationCodeLoginDTO.getPassword(), reqVerificationCodeLoginDTO.getVerificationCode(),
                reqVerificationCodeLoginDTO.getKey(), reqVerificationCodeLoginDTO.getAuthType(),
                reqVerificationCodeLoginDTO.getClientId(), reqVerificationCodeLoginDTO.getClientSecret(),
                reqVerificationCodeLoginDTO.getScope()
        );
    }

    /**
     * 获取授权码
     *
     * @param reqGetAuthorizationCodeDTO 获取授权码 DTO
     */
    @ApiOperation(value = "获取授权码", responseReference = "-")
    @GetMapping(value = "/getAuthorizationCode")
    public void getAuthorizationCode(HttpServletResponse response, @Valid ReqGetAuthorizationCodeDTO reqGetAuthorizationCodeDTO) throws IOException {
        userOauthService.getAuthorizationCode(response, reqGetAuthorizationCodeDTO);
    }

    /**
     * 授权码获取token
     *
     * @param reqGetTokenByAuthorizationCodeDTO 授权码获取token DTO
     */
    @ApiOperation(value = "授权码获取token", responseReference = "返回token")
    @PostMapping(value = "/getTokenByAuthorizationCode")
    public Result getTokenByAuthorizationCode(@Valid @RequestBody ReqGetTokenByAuthorizationCodeDTO reqGetTokenByAuthorizationCodeDTO) {
        return userOauthService.getTokenByAuthorizationCode(reqGetTokenByAuthorizationCodeDTO);
    }

    /**
     * 刷新token
     *
     * @param reqRefreshTokenDTO tokenDTO
     */
    @ApiOperation(value = "刷新token", responseReference = "返回token")
    @PostMapping(value = "/refreshToken")
    public Result refreshToken(@Valid @RequestBody ReqRefreshTokenDTO reqRefreshTokenDTO) {
        return userOauthService.refreshToken(reqRefreshTokenDTO);
    }

    /**
     * 密码模式
     * <p>手机号 + 验证码登录</p>
     *
     * @param reqPhoneLoginDTO 用户名手机号验证码登录DTO
     */
    @ApiOperation(value = "手机号 + 验证码登录", responseReference = "返回token")
    @PostMapping(value = "/loginByPhone")
    public Result loginByPhone(@Valid @RequestBody ReqPhoneLoginDTO reqPhoneLoginDTO) {
        return userOauthService.loginByPhone(reqPhoneLoginDTO);
    }

    /**
     * 用户注册
     *
     * @param reqRegisterDTO 用户注册DTO
     */
    @ApiOperation(value = "用户注册", responseReference = "返回token")
    @PostMapping(value = "/userRegister")
    public Result userRegister(@Valid @RequestBody ReqRegisterDTO reqRegisterDTO) throws InterruptedException {
        // 手机号
        String phoneNumber = reqRegisterDTO.getPhoneNumber();
        // 注册
        Result result = userOauthService.userRegister(reqRegisterDTO.getPhoneCode(), phoneNumber, reqRegisterDTO.getPassword(), reqRegisterDTO.getShareCode());
        // 注册成功直接颁发令牌
        if (result.isSuccess()) {
            log.info("注册成功直接颁发令牌 phone -> {}", phoneNumber);
            Thread.sleep(500);
            return userOauthService.getTokenByUserRegister(phoneNumber, reqRegisterDTO.getClientId(), reqRegisterDTO.getClientSecret(), reqRegisterDTO.getScope());
        }
        return result;
    }

    /**
     * 微信授权登录
     *
     * @param reqWeChatLoginDTO 微信授权登录DTO
     */
    @ApiOperation(value = "微信授权登录", responseReference = "返回token")
    @PostMapping(value = "/loginByWeChat")
    public Result loginByWeChat(@Valid @RequestBody ReqWeChatLoginDTO reqWeChatLoginDTO) {
        return userOauthService.loginByWeChat(reqWeChatLoginDTO);
    }

    /**
     *  凭证模式
     */
    @ApiOperation(value = "凭证模式登录", responseReference = "返回token")
    @PostMapping(value = "/getTokenByCredentials")
    public Result getTokenByCredentials(@Valid @RequestBody ReqGetTokenByCredentialsDTO reqGetTokenByCredentialsDTO){
        return userOauthService.getTokenByCredentials(reqGetTokenByCredentialsDTO);
    }

}
